Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.IO
Imports System.Data

Public Partial Class DisplayForm
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load

        Dim Voucher_IDC As String
        If (Request.QueryString("Voucher_IDC") IsNot Nothing) Then
            Voucher_IDC = Request.QueryString("Voucher_IDC").ToString()
        Else
            Voucher_IDC = ""
        End If

        Dim FileID As String
        If (Request.QueryString("FileID") IsNot Nothing) Then
            FileID = Request.QueryString("FileID").ToString()
        Else
            FileID = ""
        End If

        'Dim Voucher_IDC As String = If(Request.QueryString("Voucher_IDC"), "")
        Dim Voucher_IDN As Integer = Convert.ToInt32(Request.QueryString("Voucher_IDN"))
        'Dim FileID As String = If(Request.QueryString("FileID"), "")

        'Connect to the database and bring back the image contents & MIME type for the specified picture
        Using myConnection As New SqlConnection(ConfigurationManager.ConnectionStrings("DatabaseConnectionString").ConnectionString)

            Const SQL As String = "SELECT [Name], [ContentType], [Data] FROM [tblFiles] WHERE (([Voucher_IDC] = @Voucher_IDC) AND ([Voucher_IDN] = @Voucher_IDN)) And ([FileID] = @FileID)"
            Dim myCommand As New SqlCommand(SQL, myConnection)
            myCommand.Parameters.AddWithValue("@Voucher_IDC", Voucher_IDC)
            myCommand.Parameters.AddWithValue("@Voucher_IDN", Voucher_IDN)
            myCommand.Parameters.AddWithValue("@FileID", FileID)


            Dim dt As DataTable = GetData(myCommand)
            If dt IsNot Nothing And dt.Rows.Count > 0 Then
                download(dt)
            Else
                Response.Write("<script language=javascript> window.close(); </script>")


            End If
        End Using

    End Sub

	Private Function GetData(myCommand As SqlCommand) As DataTable
		Dim dt As New DataTable()
        Dim strConnString As [String] = ConfigurationManager.ConnectionStrings("DatabaseConnectionString").ConnectionString
		Dim con As New SqlConnection(strConnString)
		Dim sda As New SqlDataAdapter()
		myCommand.CommandType = CommandType.Text
		myCommand.Connection = con
		Try
			con.Open()
			sda.SelectCommand = myCommand
			sda.Fill(dt)
			Return dt
		Catch ex As Exception
			Return Nothing
		Finally
			con.Close()
			sda.Dispose()
			con.Dispose()
		End Try
	End Function

	Private Sub download(dt As DataTable)

		Dim bytes As [Byte]() = DirectCast(dt.Rows(0)("Data"), [Byte]())
		Response.Buffer = True
		Response.Charset = ""
		Response.Cache.SetCacheability(HttpCacheability.NoCache)
		Response.ContentType = dt.Rows(0)("ContentType").ToString()
        Response.AddHeader("content-disposition", "attachment;filename=" + dt.Rows(0)("Name").ToString())
		Response.BinaryWrite(bytes)
		Response.Flush()
		Response.[End]()

	End Sub
End Class

'=======================================================
'Service provided by Telerik (www.telerik.com)
'Conversion powered by NRefactory.
'Twitter: @telerik, @toddanglin
'Facebook: facebook.com/telerik
'=======================================================
